package ui.topic;

import entity.domain.value.ContinuousValue;

import javax.swing.*;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*;
import java.text.NumberFormat;

public class ContinuousTopicPanel extends TopicPanel {

    private JSlider slider_value;

    private JTextField tf_value;

    private JLabel lbl_offset;

    private JLabel lbl_dest;

    public ContinuousTopicPanel(String name, double topicWeight, int min, int max, double offset, double dest, UpdateBidInfo updateBidInfo){
        super(name, topicWeight);
        this.value = new ContinuousValue((max + min)/2);
        slider_value = new JSlider();
        slider_value.setPreferredSize(new Dimension(140, 32));
        slider_value.setMinimum(min);
        slider_value.setMaximum(max);
        int length = max - min;
        slider_value.setMajorTickSpacing((int)(length/2));
        slider_value.setMinorTickSpacing(0);
        slider_value.setPaintLabels(true);
        slider_value.setPaintTrack(true);
        slider_value.setValue(min + (int)(length/2));
        slider_value.setBackground(Color.white);
        slider_value.setForeground(Color.gray);
        tf_value = new JTextField(3);
        this.lbl_offset = new JLabel("("+String.valueOf(offset)+")");
        this.lbl_offset.setForeground(Color.gray);
        this.lbl_dest = new JLabel("("+String.valueOf(dest)+")");
        this.lbl_dest.setForeground(Color.gray);
        this.add(lbl_offset);
        this.add(slider_value);
        this.add(lbl_dest);
        this.add(tf_value);
        slider_value.addChangeListener(new ChangeListener() {
            @Override
            public void stateChanged(ChangeEvent e) {
                tf_value.setText(String.valueOf(slider_value.getValue()));
            }
        });
        tf_value.addCaretListener(new CaretListener() {
            @Override
            public void caretUpdate(CaretEvent e) {
                try{
                    String value = tf_value.getText();
                    if(value != null&&!"".equals(value)){
                        double digit = Double.parseDouble(value);
                        if(checkInRange(digit, min, max)){
                            ContinuousTopicPanel.super.value.setValue(digit);
                            slider_value.setValue((int)digit);
                        }else{
                            if(digit>max){
                                ContinuousTopicPanel.super.value.setValue(digit);
                                slider_value.setValue(max);
                            }else{
                                ContinuousTopicPanel.super.value.setValue(digit);
                                slider_value.setValue(min);
                            }
                        }
                    }
                }catch (IllegalStateException e1) {
                }catch (NumberFormatException e2){

                }finally {
                    updateBidInfo.updateBidInfo();
                }
                //System.out.println(value);
            }
        });
    }

    private boolean checkInRange(double value, int min, int max){
        return value>=min&&value<=max;
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        //frame.setContentPane(new ContinuousTopicPanel("A", 0.75,100, 500, 2.0, 1.0));
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }

}
